package com.lilanz.facepass.controller.push;

import com.alibaba.fastjson.JSONObject;
import com.lilanz.facepass.controller.push.dto.Message;
import com.lilanz.facepass.entity.client.Client;
import com.lilanz.facepass.entity.exception.MyException;
import com.lilanz.facepass.service.impl.ClientServiceImpl;
import com.lilanz.facepass.service.impl.HTTPServiceImpl;
import com.lilanz.facepass.util.Json;
import com.lilanz.facepass.util.ToolUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController()
@RequestMapping("/push")
@PropertySource({"classpath:application.properties"})//引入外部资源文件
public class PushController {
    @Value("${push.get-push-list}")
    private String getPushList;
    @Resource
    private ClientServiceImpl clientService;
    @Resource
    private HTTPServiceImpl httpService;


    @PostMapping("/pushMessage")
    public Message pushMessage(String json) {
        System.out.println("瑞为传过来的报文 " + json);
        Message message = JSONObject.parseObject(json, Message.class);
        message.setState(200);
        return message;
    }

//
//    @PostMapping("/getMessage")
//    public JSONObject getMessage() {
//        Message message = new Message();
//        message.setEquno("abc");
//        message.setAge(22);
//        Map<String, Object> namValMap = ToolUtil.getNamValMap(message, false);
//        namValMap.put("state", 200);
//        JSONObject result = httpService.sendJson("http://127.0.0.1:80/push/pushMessage", namValMap);
//        return Json.success(0, "ok", result);
//    }


    @PostMapping("/getPushList")
    public JSONObject getPushList(@RequestParam String areacode, @RequestParam String pagenumber, String persontype, String vipdate, String vipenddate) {
        Client client = clientService.selectClient();
        Map<String, String> map = new HashMap<>();
        map.put("phoneNo", client.getPhoneNo());
        map.put("password", client.getPassword());
        map.put("vi", client.getVi());
        map.put("areacode", areacode);
        map.put("pagenumber", pagenumber);
        if (persontype != null)
            map.put("persontype", persontype);
        if (vipdate != null)
            map.put("vipdate", vipdate);
        if (vipenddate != null)
            map.put("vipenddate", vipenddate);
        map.put("key", ToolUtil.getKey(map));//这个key一定放最后一个

        JSONObject result = httpService.sendPost(getPushList, client.getToken(), map);
        return Json.success(0, "ok", result);
    }


}
